home *** CD-ROM | disk | FTP | other *** search
/ OpenGL Superbible (2nd Edition) / OpenGL SuperBible e2.iso / tools / GLUT-3.7 / PROGS / GLE / CYLINDER.C < prev    next >
Encoding:
C/C++ Source or Header  |  1998-08-12  |  1.9 KB  |  93 lines

  1.  
  2. /* 
  3.  * cylinder drawing demo 
  4.  *
  5.  * FUNCTION:
  6.  * Basic demo illustrating how to write code to draw
  7.  * the most basic cylinder shape.
  8.  *
  9.  * HISTORY:
  10.  * Linas Vepstas March 1995
  11.  */
  12.  
  13. /* required include files */
  14. #include <GL/glut.h>
  15. #include <GL/tube.h>
  16.  
  17. /* the arrays in which we will store out polyline */
  18. #define NPTS 6
  19. double points [NPTS][3];
  20. float colors [NPTS][3];
  21. int idx = 0;
  22.  
  23. /* some utilities for filling that array */
  24. #define PNT(x,y,z) {             \
  25.    points[idx][0] = x;             \
  26.    points[idx][1] = y;             \
  27.    points[idx][2] = z;            \
  28.    idx ++;                \
  29. }
  30.  
  31. #define COL(r,g,b) {             \
  32.    colors[idx][0] = r;             \
  33.    colors[idx][1] = g;             \
  34.    colors[idx][2] = b;            \
  35. }
  36.  
  37. /* 
  38.  * Initialize a bent shape with three segments. 
  39.  * The data format is a polyline.
  40.  *
  41.  * NOTE that neither the first, nor the last segment are drawn.
  42.  * The first & last segment serve only to determine that angle 
  43.  * at which the endcaps are drawn.
  44.  */
  45.  
  46. void InitStuff (void) {
  47.  
  48.    /* initialize the join style here */
  49.    gleSetJoinStyle (TUBE_NORM_EDGE | TUBE_JN_ANGLE | TUBE_JN_CAP);
  50.  
  51.    COL (0.0, 0.0, 0.0);
  52.    PNT (-6.0, 6.0, 0.0);
  53.  
  54.    COL (0.0, 0.8, 0.3);
  55.    PNT (6.0, 6.0, 0.0);
  56.  
  57.    COL (0.8, 0.3, 0.0);
  58.    PNT (6.0, -6.0, 0.0);
  59.  
  60.    COL (0.2, 0.3, 0.9);
  61.    PNT (-6.0, -6.0, 0.0);
  62.  
  63.    COL (0.2, 0.8, 0.5);
  64.    PNT (-6.0, 6.0, 0.0);
  65.  
  66.    COL (0.0, 0.0, 0.0);
  67.    PNT (6.0, 6.0, 0.0);
  68. }
  69.  
  70. extern float lastx;
  71. extern float lasty;
  72.  
  73. /* draw the cylinder shape */
  74. void DrawStuff (void) {
  75.  
  76.    glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  77.  
  78.    /* set up some matrices so that the object spins with the mouse */
  79.    glPushMatrix ();
  80.    glTranslatef (0.0, 0.0, -80.0);
  81.    glRotatef (lastx, 0.0, 1.0, 0.0);
  82.    glRotatef (lasty, 1.0, 0.0, 0.0);
  83.  
  84.    /* Phew. FINALLY, Draw the polycylinder  -- */
  85.    glePolyCylinder (NPTS, points, colors, 2.3);
  86.  
  87.    glPopMatrix ();
  88.  
  89.    glutSwapBuffers ();
  90. }
  91.  
  92. /* ------------------------ end of file ------------------- */
  93.